using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;
using System.Globalization;
using System.Collections;

namespace StudentManagementApplication
{
    public partial class FrmWarningReport : Form
    {
        List<Entities.Class> classList = new List<Entities.Class>();
        List<Entities.Course> courseList = new List<Entities.Course>();
        List<Entities.Rollcall> rollcallList = new List<Entities.Rollcall>();
        Business.ClassService classService = new Business.ClassService();
        Business.CourseService courseService = new Business.CourseService();
        Business.RollcallService rollcallService = new Business.RollcallService();
        Business.PersonService personService = new Business.PersonService();
        report1 rpt = null;
        string sClassID = string.Empty;
        string sCourseID = string.Empty;
        ArrayList alStudent = null;
        Entities.Person person = null;

        public static int GetWeekNumber(DateTime dtPassed)
        {
            CultureInfo cultureInfo = CultureInfo.CurrentCulture;
            int weekNum = cultureInfo.Calendar.GetWeekOfYear(dtPassed, CalendarWeekRule.FirstFourDayWeek, DayOfWeek.Monday);
            return weekNum;
        }

        private void InitializeClass()
        {
            cmbClass.Items.Clear();
            classList = classService.GetClassByCourseID(sCourseID);
            foreach (Entities.Class _class in classList)
                cmbClass.Items.Add(_class.ClassID + "--" + _class.ClassName);
            cmbClass.SelectedIndex = 0;
        }

        private void InitializeCourse()
        {
            courseList = courseService.GetAllCourses();
            foreach (Entities.Course course in courseList)
                cmbCourse.Items.Add(course.CourseID + "--" + course.CourseName);
        }

        private bool TestListOfStudent(string sStudentID)
        {
            foreach (string sID in alStudent)
                if (sID == sStudentID)
                    return true;
            return false;
        }

        private void AnalysingRollcall()
        {
            int iCount = 0;
            int iWeek = 0;
            string sStudentID = string.Empty;
            alStudent = new ArrayList();
            using (DataTable rollcallTable = rollcallService.GetRollcallsByClassID(sClassID))
            {
                for (int i = 0; i < rollcallTable.Rows.Count; i++)
                {
                    DataRow row = rollcallTable.Rows[i];
                    if (row[3].ToString() == "0")
                    {
                        if (iCount == 0)
                        {
                            iCount++;
                            sStudentID = row[0].ToString();
                            iWeek = GetWeekNumber(DateTime.Parse(row[2].ToString()));
                        }
                        else
                        {
                            if (row[0].ToString() == sStudentID && GetWeekNumber(DateTime.Parse(row[2].ToString())) == iWeek)
                            {
                                if (!TestListOfStudent(row[0].ToString()))
                                    alStudent.Add(row[0].ToString());
                                iCount = 0;
                            }
                        }
                    }
                }
            }
        }

        public FrmWarningReport()
        {
            InitializeComponent();
        }

        private void FrmWarningReport_Load(object sender, EventArgs e)
        {
            InitializeCourse();
        }

        private void cmbClass_SelectedIndexChanged(object sender, EventArgs e)
        {
            sClassID = cmbClass.Text.Substring(0, 6);
        }

        private void cmbCourse_SelectedIndexChanged(object sender, EventArgs e)
        {
            sCourseID = cmbCourse.Text.Substring(0, 5);
            cmbClass.Enabled = true;
            InitializeClass();
        }

        private void btnShow_Click(object sender, EventArgs e)
        {
            AnalysingRollcall();
            dsReport report = new dsReport();
            foreach (string sID in alStudent)
            {
                person = personService.GetPersonByPersonID(sID);
                object[] temp = {person.PersonID,person.PersonName,person.PersonBirth };
                report.Tables["WarningStudent"].Rows.Add(temp);
            }
            rpt = new report1();
            rpt.SetDataSource(report.Tables["WarningStudent"]);
            rpt.SetParameterValue("sCourseName", cmbCourse.Text.Substring(7));
            rpt.SetParameterValue("sClassName", cmbClass.Text.Substring(8));
            crystalReportViewer1.ReportSource = rpt;
            crystalReportViewer1.Refresh();
            report.Dispose();
        }

        private void btnExit_Click(object sender, EventArgs e)
        {
            this.Close();
        }
    }
}